#!/bin/bash
#
# openQRM Enterprise developed by openQRM Enterprise GmbH.
#
# All source code and content (c) Copyright 2014, openQRM Enterprise GmbH unless specifically noted otherwise.
#
# This source code is released under the GNU General Public License version 2, unless otherwise agreed with openQRM Enterprise GmbH.
# The latest version of this license can be found here: src/doc/LICENSE.txt
#
# By using this software, you acknowledge having read this license and agree to be bound thereby.
#
#           http://openqrm-enterprise.com
#
# Copyright 2014, openQRM Enterprise GmbH <info@openqrm-enterprise.com>
#

# linuxrc for the openQRM initrd
export SHELL=/bin/bash
PATH=/sbin:/bin:/usr/bin:/usr/sbin
export PATH
# some defines 
export IMAGE_FSTAB=/mnt/etc/fstab
export IMAGE_FSTAB_STATIC=/mnt/etc/fstab-static
export NETWORK_SERVICES_TO_ADJUST="/etc/init.d/portmap /etc/init.d/netfs /etc/rc.d/init.d/portmap /etc/rc.d/init.d/netfs /etc/init.d/network /etc/rc.d/init.d/network /etc/init.d/networking /etc/rc.d/init.d/networking"
export NOT_START_SERVICE="dbus avahi-daemon kudzu"


# functions

function prepare_udev() {
	mkdir -p /mnt/initrd
	if [ ! -e /mnt/dev/console ] || [ ! -e /mnt/dev/null ]; then
		if [ ! -d /mnt/dev ]; then
			mkdir -p /mnt/dev
		fi
		mount -t tmpfs none /mnt/dev/
		cp -a /dev/* /mnt/dev/
		mkdir -p /mnt/dev/.udevdb/
	fi
	# clean udev rulez
	rm -f /mnt/etc/udev/rules.d/*
}


function prepare_var() {
	VAR_LOOP=0
	local OLD_IFS=$IFS
	IFS=$'\n'
	for var_run_dir in $(ls -al /mnt/var/run | grep ^d); do
		unset IFS
		VAR_ARRAY=($var_run_dir)
		VAR_DIR=${VAR_ARRAY[8]}
		if [ "$VAR_DIR" != "." ] && [ "$VAR_DIR" != ".." ]; then
			VAR_USER=${VAR_ARRAY[2]}
			VAR_GROUP=${VAR_ARRAY[3]}
			VAR_RUN_DIR[$VAR_LOOP]="$VAR_DIR $VAR_USER $VAR_GROUP"
			VAR_LOOP=$(( VAR_LOOP + 1 ))
		fi
	done
	mount -n -t tmpfs none /mnt/var/run
	rm -f /mnt/var/log/wtmp
	ln -s -f ../run/wtmp /mnt/var/log/wtmp
	touch /mnt/var/run/wtmp
	chmod 0664 /mnt/var/run/wtmp
	for (( d=0;d<$VAR_LOOP;d++)); do
		VAR_ARRAY=(${VAR_RUN_DIR[$d]})
		VAR_DIR=${VAR_ARRAY[0]}
		VAR_USER=${VAR_ARRAY[1]}
		VAR_GROUP=${VAR_ARRAY[2]}
		mkdir -p /mnt/var/run/$VAR_DIR
		chown $VAR_USER:$VAR_GROUP /mnt/var/run/$VAR_DIR 1>/dev/null
	done
	unset IFS
	return 0
}


function import_kernel() {
	mkdir -p /tmp_download
	mount -t tmpfs none /tmp_download
	mount -t tmpfs none /lib/modules
	cd /tmp_download
	if ! wget -q -O modules-$kernel_name.tgz http://$OPENQRM_SERVER_IP_ADDRESS/openqrm/boot-service/boot/modules-$kernel_name.tgz; then
		tftp $OPENQRM_SERVER_IP_ADDRESS -g -r boot/modules-$kernel_name.tgz -l modules-$kernel_name.tgz
	fi
	tar -C / -xzf /tmp_download/modules-$kernel_name.tgz
	rm -f modules-$kernel_name.tgz
	sync
	mkdir -p /boot
	mount -t tmpfs none /boot
	cd /boot
	if ! wget -q -O vmlinuz-$kernel_name http://$OPENQRM_SERVER_IP_ADDRESS/openqrm/boot-service/boot/vmlinuz-$kernel_name; then
		tftp $OPENQRM_SERVER_IP_ADDRESS -g -r boot/vmlinuz-$kernel_name -l vmlinuz-$kernel_name
	fi
	mv vmlinuz-$kernel_name vmlinuz-$kernel_version
	if ! wget -q -O System.map-$kernel_name http://$OPENQRM_SERVER_IP_ADDRESS/openqrm/boot-service/boot/System.map-$kernel_name; then
		tftp $OPENQRM_SERVER_IP_ADDRESS -g -r boot/System.map-$kernel_name -l System.map-$kernel_name
	fi
	mv System.map-$kernel_name System.map-$kernel_version
	cd /
	mknod /dev/ptmx c 5 2
	chmod 666 /dev/ptmx
	# and mount /dev/pts
	mkdir -p /dev/pts
	mount /dev/pts
	depmod -a
	# pcimap ?
	if [ ! -f /lib/modules/$KERNEL_VERSION/modules.pcimap ]; then
		depmod -a -m
		if [ ! -f /lib/modules/$KERNEL_VERSION/modules.pcimap ]; then
			echo "WARNING: Could not create /lib/modules/$KERNEL_VERSION/modules.pcimap!"
			echo "WARNING: Automatic hardware-detection may fail!"
		fi
	fi
	# manually load ide disk + cdroms
	modprobe ide-core 2>/mplog1
	modprobe ide-generic 2>/mplog1
	modprobe ide-disk 2>/mplog1
	modprobe ata_piix 2>/mplog1
	modprobe sata_nv 2>/mplog1
	modprobe sd_mod 2>/mplog1
	rm -f /mplog1
	echo "- (2) Starting hardware detection (all system components)"
	for module in `/sbin/pcimodules`; do
		if [ "$module" == "matroxfb_base" ]; then
			# skip this, it makes the kvm vm vnc console unreadable
			continue
		fi
		if [ "$module" == "cirrusfb" ]; then
			# skip this, it crashes the xen vm qemu-dm when accessing the vnc console on debian-lenny
			continue
		fi
		echo " -- loading $module"
		modprobe -s -k "$module"
	done
	umount /tmp_download
	rmdir /tmp_download
	# start udev
	udevd --daemon
	# give time to settle, especially needed for some scsi devices
	sleep 3
	#lvm
	if [ -x /bin/lvm ]; then
		if /bin/lvm lvdisplay | grep -E "LV Path" 1>/dev/null; then
			LVM_VOLME_GREP="LV Path"
		else
			LVM_VOLME_GREP="LV Name"
		fi
		echo "- Scanning for physical lvm drives"
		/bin/lvm pvscan
		echo "- Scanning for logical volume groups"
		/bin/lvm vgscan
		echo "- Activating logical volume groups"
		/bin/lvm vgchange -ay
		echo "- Scanning for logical volumes"
		/bin/lvm lvscan
		echo "- Activating logical volumes"
		for LVOL in `/bin/lvm lvdisplay | grep -i "$LVM_VOLME_GREP" | awk {' print $3 '}`; do
			echo "- Activating $LVOL"
			/bin/lvm lvchange -ay -y $LVOL
		done
	fi
}


# this functions adds the common entries to the images fstab
function create_fstab_commons() {
	local OLD_IFS=$IFS
	echo "none	 /dev/pts     devpts   gid=5,mode=620     0 0" >> $IMAGE_FSTAB
	echo "none	 /proc	proc     defaults	   0 0" >> $IMAGE_FSTAB
	echo "none	 /dev/shm     tmpfs    defaults	   0 0" >> $IMAGE_FSTAB
	echo "/dev/fd0     /mnt/floppy  auto     noauto,owner,kudzu 0 0" >> $IMAGE_FSTAB
	IFS=$'\n'
	if [ -d /proc/ide ]; then
		for IDEDEVICES in $(ls /proc/ide); do
			if grep cdrom /proc/ide/$IDEDEVICES/media 1>/dev/null 2>&1; then
				echo "Found cdrom at $IDEDEVICES"
				mkdir -p /mnt/cdrom$LOOP 1>/dev/null
				echo "/dev/$IDEDEVICES     /mnt/cdrom$LOOP   udf,iso9660 noauto,owner,kudzu,ro 0 0" >> $IMAGE_FSTAB
				LOOP=$(( LOOP +1 ))
			fi
		done
	fi
	FOUND_SWAP=0
	for SWPSPACE in $(cat /proc/partitions |grep '[0-9]' | awk '{print $4}'); do
		if swapon /dev/${SWPSPACE} 1>/dev/null 2>&1; then
			swapoff /dev/${SWPSPACE} 1>/dev/null
			echo "Adding /dev/${SWPSPACE} as swap space"
			echo "/dev/${SWPSPACE} swap	swap	defaults	0 0" >> $IMAGE_FSTAB
			FOUND_SWAP=1
		fi
	done
	if [ "$FOUND_SWAP" == "0" ]; then
		# we need to create a small swap file
		dd if=/dev/zero of=/mnt/tmp/mini-swap.swap bs=1M count=1
		mkswap /mnt/tmp/mini-swap.swap
		echo "Adding mini-swap file as swap space"
		echo "/tmp/mini-swap.swap swap	swap	noauto	0 0" >> $IMAGE_FSTAB
	fi
	IFS=$OLD_IFS
	if [ -f $IMAGE_FSTAB_STATIC ]; then
		echo "Found $IMAGE_FSTAB_STATIC, adding to $IMAGE_FSTAB"
		cat $IMAGE_FSTAB_STATIC >> $IMAGE_FSTAB
	fi

}


function set_root_password() {
	local id=$1
	if $WGET -q -O /iauth.$id $openqrm_web_protocol://$OPENQRM_SERVER_IP_ADDRESS/openqrm/action/image-auth/iauth.$id.php 2>/iauth.log; then
		cryptedpassword=`cat /iauth.$id`
		rm -f /iauth.$id iauth.log
		if [ "$cryptedpassword" != "" ]; then
			sed -i "s#^root:[^:]*#root:$cryptedpassword#" /mnt/etc/shadow
			sed -i "s#^root:[^:]*#root:$cryptedpassword#" /mnt/etc/shadow-
		fi
	fi
}

function prepare_services() {
	chroot /mnt ln -sf /$resource_basedir/openqrm/etc/init.d/openqrm-client /etc/init.d/openqrm-client
	echo "#!/bin/bash -x" > /mnt/tmp/openqrm-client.enable
	echo "OPENQRM_SERVER_BASE_DIR=$resource_basedir" >> /mnt/tmp/openqrm-client.enable
	echo ". /$resource_basedir/openqrm/include/openqrm-functions" >> /mnt/tmp/openqrm-client.enable
	echo "openqrm_chkconfig add openqrm-client" >> /mnt/tmp/openqrm-client.enable
	for SERVICE in $NOT_START_SERVICE; do
		echo "openqrm_chkconfig del $SERVICE" >> /mnt/tmp/openqrm-client.enable
	done
	chmod +x /mnt/tmp/openqrm-client.enable
	chroot /mnt /tmp/openqrm-client.enable 1>/dev/null 2>&1
	rm -f /mnt/tmp/openqrm-client.enable
}


function prepare_net_init() {
	INIT_SCRIPTS_TO_PREPARE="$@"
	for INIT_SCRIPT in $INIT_SCRIPTS_TO_PREPARE; do
		if [ -f /mnt/$INIT_SCRIPT ]; then
			if ! grep -A2 "stop)" /mnt/$INIT_SCRIPT | grep -q "openqrm" ; then
				echo "Preparing init script $INIT_SCRIPT"
				sed -e "s/^[ \t]*stop)\(.*\)/stop) exit # added by openqrm \n\1/" /mnt/$INIT_SCRIPT > /mnt/${INIT_SCRIPT}.openqrm
				mv -f /mnt/${INIT_SCRIPT}.openqrm /mnt/$INIT_SCRIPT
				chmod +x /mnt/$INIT_SCRIPT
			fi
		fi
	done
}






# main
mount -t proc proc /proc
# creating the devices
echo "Creating the devices ..."
makedevs -d /etc/initrd-devices.conf /
mkdir -p /dev/fd
ln -sf /proc/self/fd /dev/fd
mknod /dev/tty c 5 0
ln -sf /dev/ram1 /dev/ram
mknod /dev/random c 1 8
mknod /dev/urandom c 1 9
echo 0x0100 > /proc/sys/kernel/real-root-dev
echo 6 > /proc/sys/kernel/printk
syslogd &&  klogd
# need to run depmod 2 times as experienced
depmod -a
depmod -a
KERNEL_VERSION=`uname -r`
if [ ! -f /lib/modules/$KERNEL_VERSION/modules.pcimap ]; then
	# create the legacy maps
	echo " - /lib/modules/$KERNEL_VERSION/modules.pcimap missing. Trying to create it"
	depmod -a -m
	if [ -f /lib/modules/$KERNEL_VERSION/modules.pcimap ]; then
		echo " - /lib/modules/$KERNEL_VERSION/modules.pcimap created"
	else
		echo "WARNING: Could not create /lib/modules/$KERNEL_VERSION/modules.pcimap!"
		echo "WARNING: Automatic hardware-detection may fail!"
	fi
fi
# load virtio drivers for KVM + Xen VMs
for module in xen-netfront virtio virtio_pci virtio_ring virtio_net crc32c; do
    echo " -- loading $module"
    modprobe "$module" 2>/mplog
done
rm -f  /mplog
if grep -q sysfs /proc/filesystems ; then
    mount -t sysfs none /sys
fi
# firmware rules existing ?
if [ ! -f /etc/udev/rules.d/50-firmware.rules ]; then
	# debian/ubuntu
	if [ -x /lib/udev/firmware ]; then
		echo 'SUBSYSTEM=="firmware", ACTION=="add", RUN+="firmware"' > /etc/udev/rules.d/50-firmware.rules
	fi
	# centos/rh
	if [ -x /lib/udev/firmware.sh ]; then
		echo 'SUBSYSTEM=="firmware", ACTION=="add", RUN+="firmware.sh"' > /etc/udev/rules.d/50-firmware.rules
	fi
fi
udevd --daemon
# start acpid early if existing
if which acpid 1>/dev/null; then
	echo "Starting acpid daemon"
	acpid
fi
sleep 4

if [ -f /lib/modules/$KERNEL_VERSION/modules.pcimap ]; then
	echo "- (1) Starting hardware detection (network)"
	for module in `/sbin/pcimodules`; do
		echo " -- loading $module"
		modprobe -s -k "$module"
	done
else
	echo "- (1) Starting hardware detection (network)"
	echo "NOTICE: No modules.pcimap available. Probing all network devices"
	for module in `find /lib/modules/$KERNEL_VERSION/* -name "*.ko"`; do
		module=`basename $module | cut -d'.' -f1`
		echo " -- loading $module"
		modprobe -s -k "$module"
	done
fi

# bnx2x support, bring up the faster nics first as eth0/1
if lsmod | grep ^bnx2x 1>/dev/null; then
	rmmod tg3
	rmmod bnx2x
	udevd --daemon
	modprobe bnx2x
	ifconfig eth0 up
	ifconfig eth1 up
	modprobe tg3
fi
eval `cat /proc/cmdline`
sleep 3
killall udevd 2>/mplog
rm -f  /mplog
sleep 3
# collecting some infos from /proc/cmdline
OPENQRM_RESOURCE_MAC_ADDRESS=`echo $BOOTIF | sed -e "s/-/:/g"  | sed -e "s/^01://g" | tr '[:lower:]' '[:upper:]'`
OPENQRM_RESOURCE_MANAGED_INTERFACE=`ifconfig -a | grep $OPENQRM_RESOURCE_MAC_ADDRESS | awk {' print $1 '} | head -n 1`
OPENQRM_RESOURCE_IP_ADDRESS=`echo $ip | cut -d':' -f1`
OPENQRM_SERVER_IP_ADDRESS=`echo $ip | cut -d':' -f2`
OPENQRM_RESOURCE_DEFAULT_GATEWAY=`echo $ip | cut -d':' -f3`
OPENQRM_RESOURCE_SUBNETMASK=`echo $ip | cut -d':' -f4`
OPENQRM_RESOURCE_BROADCAST=`ipcalc -b $OPENQRM_RESOURCE_IP_ADDRESS $OPENQRM_RESOURCE_SUBNETMASK | cut -d'=' -f2`

OPENQRM_RESOURCE_PARAMETER_FILE="/var/openqrm/openqrm-resource.conf"
mkdir -p `dirname $OPENQRM_RESOURCE_PARAMETER_FILE`

# adjust arp settings
if [ -f /proc/sys/net/ipv4/conf/$OPENQRM_RESOURCE_MANAGED_INTERFACE/arp_filter ]; then
	echo 1 > /proc/sys/net/ipv4/conf/$OPENQRM_RESOURCE_MANAGED_INTERFACE/arp_filter
fi
if [ -f /proc/sys/net/ipv4/conf/$OPENQRM_RESOURCE_MANAGED_INTERFACE/rp_filter ]; then
	echo 1 > /proc/sys/net/ipv4/conf/$OPENQRM_RESOURCE_MANAGED_INTERFACE/rp_filter
fi

# get the net working
echo "Bringing up $OPENQRM_RESOURCE_MANAGED_INTERFACE:oq"
echo "-> mac-address		: $OPENQRM_RESOURCE_MAC_ADDRESS"
echo "-> ip-address		: $OPENQRM_RESOURCE_IP_ADDRESS"
echo "-> subnetmask		: $OPENQRM_RESOURCE_SUBNETMASK"
echo "-> broadcast		: $OPENQRM_RESOURCE_BROADCAST"
echo "-> default gw		: $OPENQRM_RESOURCE_DEFAULT_GATEWAY"
echo "-> openQRM-server	: $OPENQRM_SERVER_IP_ADDRESS"
ifconfig lo 127.0.0.1 up
ifconfig $OPENQRM_RESOURCE_MANAGED_INTERFACE down
ifconfig $OPENQRM_RESOURCE_MANAGED_INTERFACE up
ifconfig $OPENQRM_RESOURCE_MANAGED_INTERFACE:oq $OPENQRM_RESOURCE_IP_ADDRESS netmask $OPENQRM_RESOURCE_SUBNETMASK broadcast $OPENQRM_RESOURCE_BROADCAST up
if [ "$OPENQRM_RESOURCE_DEFAULT_GATEWAY" != "0.0.0.0" ]; then
	route add default gw $OPENQRM_RESOURCE_DEFAULT_GATEWAY
fi
# force a small delay for the networkcard to initialize
ping -c 5 -q $OPENQRM_SERVER_IP_ADDRESS > /dev/null

# get resource parameter
echo "Getting resource-parameters ..."
if [ "$id" == "-1" ]; then
	echo "New resource detected ! Adding to the openQRM-server at $OPENQRM_SERVER_IP_ADDRESS"
fi	
# sending resource-id will add the resource if not existing (-1 = new resource)
# loop get-parameters in case the network card is still not ready initialized
NETWORKCARD_LOOP=0

while (true); do
	# try https first
	if wget --no-check-certificate --random-file=/dev/random -q -O $OPENQRM_RESOURCE_PARAMETER_FILE "https://$OPENQRM_SERVER_IP_ADDRESS/openqrm/action/resource-monitor.php?resource_command=get_parameter&resource_id=$id&resource_mac=$OPENQRM_RESOURCE_MAC_ADDRESS&resource_ip=$OPENQRM_RESOURCE_IP_ADDRESS&resource_subnet=$OPENQRM_RESOURCE_SUBNETMASK"; then
		if ! grep "<html>" $OPENQRM_RESOURCE_PARAMETER_FILE >/out; then
			echo "-> got parameters via https"
			break
		fi
		rm -f /out
	fi
	# try http
	if wget -q -O $OPENQRM_RESOURCE_PARAMETER_FILE "http://$OPENQRM_SERVER_IP_ADDRESS/openqrm/action/resource-monitor.php?resource_command=get_parameter&resource_id=$id&resource_mac=$OPENQRM_RESOURCE_MAC_ADDRESS&resource_ip=$OPENQRM_RESOURCE_IP_ADDRESS&resource_subnet=$OPENQRM_RESOURCE_SUBNETMASK"; then
		if ! grep "<html>" $OPENQRM_RESOURCE_PARAMETER_FILE >/out; then
			echo "-> got parameter via http"
			break
		fi
		rm -f /out
	fi
	# no chance ?
	if [ "$NETWORKCARD_LOOP" == "10" ]; then
		echo "ERROR: Could not reach the openQRM-Server at $OPENQRM_SERVER_IP_ADDRESS!"
		# give a shell for the admin
		/bin/bash
	fi
	NETWORKCARD_LOOP=$(( NETWORKCARD_LOOP + 1 ))
	sleep 5
done
# source parameters
. $OPENQRM_RESOURCE_PARAMETER_FILE

# define wget
if [ "$openqrm_web_protocol" == "https" ]; then
	export WGET="wget --no-check-certificate --random-file=/dev/random"
else
	export WGET="wget"
fi

# get kernel + modules by tftp
echo "Importing kernel files"
import_kernel
# load usb modules
for module in usbcore hid usbhid; do
    echo " -- loading $module"
    modprobe "$module" 2>/mplog
done
rm -f  /mplog
# run default openqrm-client boot-service
KERNEL_ARCH=`uname -m`
if echo $KERNEL_ARCH | grep i.*86 1>/dev/null; then
	# i386
	OPENQRM_CLIENT_ARCH=i386
else
	OPENQRM_CLIENT_ARCH=x86_64
fi

cd /
if ! $WGET -q -O openqrm-client.tgz "$openqrm_web_protocol://$OPENQRM_SERVER_IP_ADDRESS/openqrm/boot-service/openqrm-client.tgz"; then
	rm -f openqrm-client.tgz
	echo "WARNING: Failed to get the openqrm-client package! Starting an emergency shell"
	/bin/bash
fi
tar -xzf openqrm-client.tgz
rm -f openqrm-client.tgz
$resource_basedir/openqrm/etc/init.d/openqrm-client start

if [ "$image_type" == "ram" ] || [ "$image_type" == "" ]; then

	# send idle/available
	echo "Sending idle/available to openQRM-server at $OPENQRM_SERVER_IP_ADDRESS"
	$WGET -q -O /dev/null "$openqrm_web_protocol://$OPENQRM_SERVER_IP_ADDRESS/openqrm/action/resource-monitor.php?resource_command=update_status&resource_id=$resource_id&resource_state=active&resource_event=available"
	hostname idle$resource_id
	# rcs0 will be started here from init
	# for initramfs initrds we start a shell here
	if [ -x /init ]; then
		echo "Detected initrd as initramfs, starting a shell"
		exec setsid /bin/bash -c 'exec /bin/bash </dev/tty1 >/dev/tty1 2>&1'
	fi

else
	# if any deployment plugin is enabled, handle over the control
	# -> deployment plugins will take care to mount the root fs
	#    and do the pivot_root if needed

	# pre-root-mount-hook to setup everything which needs to be done before the root-mount
	cd /
	for boot_service in $openqrm_boot_services; do
		if $WGET -q -O /pre-root-mount-hook.$boot_service "$openqrm_web_protocol://$OPENQRM_SERVER_IP_ADDRESS/openqrm/boot-service/pre-root-mount-hook.$boot_service"; then
			echo " - Running for pre-root-mount-hook of boot-service $boot_service"
			chmod +x pre-root-mount-hook.$boot_service
			. ./pre-root-mount-hook.$boot_service
			pre_mount_rootfs
		else
			rm -f /pre-root-mount-hook.$boot_service
		fi
	done

	# root-mount-hook
	eval $image_deployment_parameter
	cd /
	$WGET -q "$openqrm_web_protocol://$OPENQRM_SERVER_IP_ADDRESS/openqrm/boot-service/root-mount.$image_type"
	chmod +x root-mount.$image_type
	# source the deployment plugin command, it will provide
	# the functions to mount/remount the rootfs at /mnt according
	# to the deployment-type and the image_deployment_parameters
	. ./root-mount.$image_type

	# mount ro (provided by the deployment plugin)
	###############################################################
	# DEPLOYMENT PLUGIN HOOK ######################################
	###############################################################
	mount_rootfs

	# here we have the rootfs mounted rw so we get the openQRM client according
	# distro and arch
	# find out the short distri name
	if [ -f /mnt/etc/debian_version ]; then
		# debian or ubuntu, try to find out without lsb-release which may not be installed
		if grep -i ubuntu /mnt/etc/apt/sources.list 1>/dev/null; then
			OPENQRM_SHORT_DISTRI_NAME="ubuntu"
		else
			OPENQRM_SHORT_DISTRI_NAME="debian"
		fi
	elif [ -f /mnt/etc/redhat-release ]; then
		OPENQRM_SHORT_DISTRI_NAME="centos"
	elif [ -f /mnt/etc/SuSE-release ]; then
		OPENQRM_SHORT_DISTRI_NAME="suse"
	else
		OPENQRM_SHORT_DISTRI_NAME="custom"
	fi
	# get the openQRM client package fitting to distri + arch
	rm -rf /mnt/$resource_basedir/openqrm
	cd /
	echo "Getting openqrm-client package"
	if ! $WGET -q -O /mnt/openqrm-client.tgz "$openqrm_web_protocol://$OPENQRM_SERVER_IP_ADDRESS/openqrm/boot-service/openqrm-client.tgz"; then
		rm -f /mnt/openqrm-client.tgz
		echo "WARNING: Failed to get the openqrm-client package!"
		# copy openQRM client which is on the initrd
		mkdir /mnt/$resource_basedir/openqrm/
		cp -a /$resource_basedir/openqrm/* /mnt/$resource_basedir/openqrm/
	else
		# use the openQRM client which fits to the distri and arch
		cd /mnt/
		tar -xzf openqrm-client.tgz
		rm -f openqrm-client.tgz
		cd /
	fi

	# fill /boot and /lib/modules on the fs-image
	echo "Filling /boot and /lib/modules"
	rm -rf /mnt/boot/vmlinuz-$kernel_version /mnt/boot/System.map-$kernel_version /mnt/lib/modules/$kernel_version
	cp -af /boot/vmlinuz-$kernel_version /mnt/boot/vmlinuz-$kernel_version
	cp -af /boot/System.map-$kernel_version /mnt/boot/System.map-$kernel_version
	cp -af /lib/modules/$kernel_version /mnt/lib/modules/

	# create custom fstab / needed for the remount rw during the system init
	# (custom entry provided by the deployment plugin)
	###############################################################
	# DEPLOYMENT PLUGIN HOOK ######################################
	###############################################################
	create_fstab

	# (common entries provided by a local function)
	create_fstab_commons

	# transfer resource basedir
	echo "Preparing resource openQRM client"

	# copy resource parameter
	mkdir -p /mnt/`dirname $OPENQRM_RESOURCE_PARAMETER_FILE`
	cp -af $OPENQRM_RESOURCE_PARAMETER_FILE /mnt/$OPENQRM_RESOURCE_PARAMETER_FILE

	# prepare init according plugin services
	echo "Preparing system init"
	prepare_udev
	prepare_var
	prepare_services
	prepare_net_init $NETWORK_SERVICES_TO_ADJUST

	# managed shared image
	if [ "$image_isshared" != "" ]; then
		echo "Prepare image as shared"
	fi
	# set root password
	set_root_password $image_id

	# remount rw (provided by the deployment plugin)
	###############################################################
	# DEPLOYMENT PLUGIN HOOK ######################################
	###############################################################
	remount_rootfs

	# chroot + init
	killall syslogd klogd 1>/dev/null 2>&1
	killall udevd 1>/dev/null 2>&1
	killall acpid 1>/dev/null 2>&1
	sleep 2
	if grep sysfs /proc/filesystems 1>/dev/null ; then
		umount /sys
	fi
	umount /proc
	umount /boot
	umount /lib/modules

	exec switch_root /mnt /sbin/init
	# here the system will do a normal init
	# from the mounted rootfs image
fi

